package lvcy.message.sender.builder.impl;

import java.io.ByteArrayOutputStream;
import java.lang.reflect.Constructor;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import lvcy.message.entity.RequestEntity;
import lvcy.message.sender.JstMessageSender;
import lvcy.message.sender.MessageSender;
import lvcy.message.sender.builder.JstMessageSenderBuilder;

public class JstMultiMessageSenderBuilder extends JstMessageSenderBuilder{

	private Logger log=LogManager.getLogger(this);
	@Override
	public MessageSender build(RequestEntity request) {
	
		String postData=getPostData(request);
		try {
			Class<?> clazz=Class.forName(JstMessageSender.class.getName());
			Constructor<?> constructor=clazz.getDeclaredConstructor(String.class,String.class);
			this.sender=(JstMessageSender) constructor.newInstance("",postData);
		} catch (Exception e) {
			log.error(e);
		}
		return this.sender;
	}
	
	private String getPostData(RequestEntity request){
		String opKind="51";
		String interFaceID="";
		String serType="短信类型";
		String sendTime=request.getExtras().get("sendTime")==null?"":request.getExtras().get("sendTime").toString();
		try {
			Document document=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
			
			Element group=document.createElement("Group");
			group.setAttribute("Login_Name", request.getAccount());
			group.setAttribute("Login_Pwd", request.getPassword());
			
			group.setAttribute("OpKind", opKind);
			group.setAttribute("InterFaceID", interFaceID);
			group.setAttribute("SerType", serType);
			
			Element eTime=document.createElement("E_Time");
			eTime.setTextContent(sendTime);
			group.appendChild(eTime);
			
			Element mobile=document.createElement("Mobile");
			mobile.setTextContent(getPhoneList(request.getPhones()));
			group.appendChild(mobile);
			
			Element content=document.createElement("Content");
			group.appendChild(content);
			
			CDATASection cdataSection=document.createCDATASection(request.getContent());
			content.appendChild(cdataSection);
			if(!request.getExtras().containsKey("clientId")){
				throw new IllegalArgumentException("The clientID must not be null!");
			}
			Element clientId=document.createElement("ClientID");
			clientId.setTextContent(request.getExtras().get("clientId").toString());
			group.appendChild(clientId);
			
			TransformerFactory transformerFactory=TransformerFactory.newInstance();
			Transformer transformer=transformerFactory.newTransformer();
			transformer.setOutputProperty("encoding", "UTF-8");
			ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
			transformer.transform(new DOMSource(group), new StreamResult(byteArrayOutputStream));
			
			return byteArrayOutputStream.toString();
			
		} catch (ParserConfigurationException | TransformerException e) {
			log.error(e);
		}
		
		return null;
	}
	private String getPhoneList(List<String> phones){
		StringBuilder builder=new StringBuilder();
		for(String phone:phones){
			builder.append(phone).append(",");
		}
		return builder.toString();
	}

}
